package com.itheima.web.controller;

import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.util.StrUtil;
import com.itheima.domain.db.Admin;
import com.itheima.web.manager.AdminManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @author zsk
 * @create 2022-03-24-15:48
 */
@RestController
public class AdminController {

    @Autowired
    private AdminManager adminManager;

    //登录时获取验证码图片
    @GetMapping("/system/users/verification")
    public void verification(String uuid, HttpServletResponse response) throws IOException {

        //调用manager
        LineCaptcha lineCaptcha = adminManager.verification(uuid);

        //返回结果
        lineCaptcha.write(response.getOutputStream());
    }

    //登录验证
    @PostMapping("/system/users/login")
    public ResponseEntity login(@RequestBody Map<String, String> param) {
        //接收参数
        String username = param.get("username");
        String password = param.get("password");
        String verificationCode = param.get("verificationCode");
        String uuid = param.get("uuid");

        //调用manager
        return adminManager.login(username, password, verificationCode, uuid);

    }

    //校验token信息
    @PostMapping("/system/users/profile")
    public ResponseEntity profile(@RequestHeader("Authorization") String token) {

        //调用manager
        Admin admin = adminManager.findAdminByToken(token);

        //返回
        return ResponseEntity.ok(admin);

    }

    //用户退出
    @PostMapping("/system/users/logout")
    public void logout(@RequestHeader("Authorization") String token) {

        //调用manager
        adminManager.logout(token);

    }

    //用户列表
    @GetMapping("/manage/users")
    public ResponseEntity findUserInfoByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        //调用manager
        return adminManager.findUserInfoByPage(pageNum, pageSize);

    }

    //用户详情列表
    @GetMapping("/manage/users/{userId}")
    public ResponseEntity users(@PathVariable Long userId) {

        //调用manager
        return adminManager.users(userId);
    }

    //用户动态详情
    @GetMapping("/manage/messages")
    public ResponseEntity messages(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "uid", required = false) Long userId,
            String state//审核状态
    ) {
        //处理状态
        Integer stateInt = null;
        //如果state不为''和不为空字符串
        if (!StrUtil.equals(state, "''") && StrUtil.isNotEmpty(state)) {
            stateInt = Integer.parseInt(state);//state就等于null
        }
        //调用manager
        return adminManager.messages(userId, stateInt, pageNum, pageSize);

    }

    //用户动态记录
    @GetMapping("/manage/messages/{publishId}")
    public ResponseEntity findMovementVoById(@PathVariable String publishId) {

        //调用manager
        return adminManager.findMovementVoById(publishId);

    }

    //用户评论详情
    @GetMapping("/manage/messages/comments")
    public ResponseEntity findCommentVoByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "messageID") String publishId

    ){
        //调用manager
        return adminManager.findCommentVoByPage(publishId,pageNum,pageSize);

    }

    //用户视频详情
    @GetMapping("/manage/videos")
    public ResponseEntity findByUserVideos(
            @RequestParam(value = "page",defaultValue = "1")Integer pageNum,
            @RequestParam(value = "pagesize",defaultValue = "10")Integer pageSize,
            @RequestParam(value = "uid") Long userId
    ){
        //调用manager
        return adminManager.findByUserVideos(userId,pageNum,pageSize);

    }


}
